(function() {
    var myApp = angular.module('myApp');

    myApp.controller("myUserController", function($scope, $rootScope, $log, myUserTableOption, reqDataByAjax) {
        // TAB显示之后，调整表格大小
        $scope.$on("shown.bs.tab", function(event, element, target) {
            if (target.is('#userlist')) {
                $scope.$root.$broadcast('resize.table', '#usertable');
            }
        });

        // 设置用户表Option
        $scope.option = myUserTableOption;
        $scope.option.ajax = ajax;

        function ajax(data, callback, settings) {
            reqDataByAjax("/scheduling/api/user/user/getall", data).then(function (ret) {
                callback({
                    "draw": data.draw,
                    "recordsTotal": ret[1][0].recordsTotal,
                    "recordsFiltered": ret[2][0].recordsFiltered,
                    "data": ret[0]
                });
            });
        }

        // 记录当前选中用户
        $scope.$root.selectedUsers = [];
        $scope.$on("select", function(event, element, e, dt, type, indexes) {
            if (element.is("#usertable"))
                $scope.$root.selectedUsers = Array.prototype.slice.call(dt.rows('.selected').data(), 0);
        });
        $scope.$on("deselect", function(event, element, e, dt, type, indexes) {
            if (element.is("#usertable"))
                $scope.$root.selectedUsers = Array.prototype.slice.call(dt.rows('.selected').data(), 0);
        });
        $scope.$on("draw.dt", function(event, element, e, settings) {
            if (element.is("#usertable"))
                $scope.$root.selectedUsers = [];
        });

    });

    myApp.controller("createUserController", function($scope, $log, reqDataByAjax) {
        // TAB显示之前，设置待显示的用户信息
        $scope.$on("show.bs.tab", function(event, element, target) {
            if (target.is('#createuser')) {
                $scope.user = {};
            }
        });

        // 保存输入的用户信息
        $scope.save = function(e) {
            $scope.user.user_name = $scope.user.username;
            reqDataByAjax("/scheduling/api/user/user/createuser", $scope.user, "POST").then(function(data) {
                // 保存后刷新用户列表
                $scope.$root.$broadcast("refresh.table", "#usertable");
            });
        };
    });

    myApp.controller("modifyUserController", function($scope, $log, reqDataByAjax) {
        // TAB显示之前，设置待显示的用户信息
        $scope.$on("show.bs.tab", function(event, element, target) {
            if (target.is('#modifyuser')) {
                $scope.user = $scope.$root.selectedUsers[0];
            }
        });

        // 保存修改后的用户信息
        $scope.save = function(e) {
            $scope.user.user_name = $scope.user.username;
            reqDataByAjax("/scheduling/api/user/user/modifyuser", $scope.user, "POST").then(function(data) {
                // 保存后刷新用户列表
                $scope.$root.$broadcast("refresh.table", "#usertable");
            });
        };
    });

    myApp.controller("deleteUserController", function($scope, $log, reqDataByAjax) {
        $scope.ok = function() {
            reqDataByAjax("/scheduling/api/user/user/deleteuser", $scope.$root.selectedUsers, "POST").then(function() {
                // 保存后刷新用户列表
                $scope.$root.$broadcast("refresh.table", "#usertable");
            });
        };
    });

    myApp.controller("myUserRoleController", function($scope, $log
        , myRoleTableOption, reqDataByAjax
    ) {
        // 切换至角色表页面时，刷新角色表
        $scope.$on("shown.bs.tab", function(event, element, target) {
            if (target.is("#setroles"))
                $scope.$root.$broadcast("refresh.table", "#roletable");
        });

        // 设置角色表Option
        $scope.option = myRoleTableOption;
        $scope.option.ajax = ajax;
        function ajax(data, callback, settings) {
            data.user_id = $scope.$root.selectedUsers && $scope.$root.selectedUsers.length && $scope.$root.selectedUsers[0].user_id;
            reqDataByAjax("/scheduling/api/user/user/getallroles", data).then(function (ret) {
                callback({
                    "draw": data.draw,
                    "recordsTotal": ret[1][0].recordsTotal,
                    "recordsFiltered": ret[2][0].recordsFiltered,
                    "data": ret[0]
                });
            });
        }

        // 设置角色表默认选中
        $scope.$on("draw.dt", function(event, element, e, settings) {
            if (!element.is('#roletable'))
                return;

            var table = $.fn.dataTable.tables({ visible: false, api: true }).table(element);
            if (!table.node())
                return;

            var si = [];
            table.rows().every( function ( rowIdx, tableLoop, rowLoop ) {
                var d = this.data();
                d.selected && si.push(rowIdx);
            });

            table.rows(si).select();
        });

        // 记录当前选中角色
        $scope.roles = [];
        $scope.$on("select", function (event, element, e, dt, type, indexes) {
            if (element.is("#roletable"))
                $scope.roles = Array.prototype.slice.call(dt.rows('.selected').data(), 0);
        });

        $scope.$on("deselect", function (event, element, e, dt, type, indexes) {
            if (element.is("#roletable"))
                $scope.roles = Array.prototype.slice.call(dt.rows('.selected').data(), 0);
        });

        $scope.$on("draw.dt", function (event, element, e, settings) {
            if (element.is("#roletable"))
                $scope.roles = [];
        });

        // 保存用户角色设置
        $scope.ok = function() {
            var user_id = $scope.$root.selectedUsers[0].user_id;
            var roles   = $scope.roles;
            var url;
            if (roles.length == 0) {
                roles.push({ user_id : user_id });
                url = "/scheduling/api/user/user/clearuserroles";
            } else {
                for (var r in roles)
                    roles[r].user_id = user_id;
                url = "/scheduling/api/user/user/saveuserroles";
            }
            reqDataByAjax(url, roles, "POST");
        };
    });

    // 重置密码
    myApp.controller("resetpasswordController", function($scope, $log, reqDataByAjax) {
        $scope.ok = function() {
            reqDataByAjax("/scheduling/api/user/user/resetuserpassword", $scope.$root.selectedUsers, "POST").then(function() {
                // 保存后刷新用户列表
                $scope.$root.$broadcast("refresh.table", "#usertable");
            });
        };
    });

})();